package com.sh169.action.admin;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.lang.Strings;
import org.nutz.lang.random.StringGenerator;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.UploadAdaptor;

import com.sh169.beans.PageForm;
import com.sh169.pojos.Article;
import com.sh169.pojos.SpecialVendor;
import com.sh169.util.ApplicationUtil;

@IocBean(name="specialVendorController")
@InjectName("specialVendorController")
@At("/admin/specialVendor")
public class SpecialVendorController {

	@Ok("tiles:admin.specialVendor.list")
	public PageForm<SpecialVendor> list(@Param("page")int num){
		
		int total = dao.count(SpecialVendor.class);
		if(total == 0) return null;
		if(num<1) num = 1;
		else if((total%20+1)<num)
			num = 1;
		Pager page = dao.createPager(num, 20);
		List<SpecialVendor> list = dao.query(SpecialVendor.class, null, page);
		PageForm<SpecialVendor> pf = new PageForm<SpecialVendor>();
		pf.setPage(page);
		pf.setResultList(list);
		return pf;
	}
	
	@Ok("tiles:admin.specialVendor.create")
	public void create(){
	}
	@Ok("redirect:/admin/specialVendor/list.do")
	@AdaptBy(type=UploadAdaptor.class,args={"E:/TomcatShare/temp"})
	public void save(@Param("::o.")SpecialVendor vendor,@Param("file")File image) throws IOException{
		
		if(image!=null && !Strings.isEmpty(Files.getSuffixName(image))){
			String imagepath = "/images/vendorlogo/"+new StringGenerator(32, 32).next()+"."+Files.getSuffixName(image);
			Files.copyFile(image, new File(ApplicationUtil.getWebHomePath()+imagepath));
			vendor.setLogo(imagepath);
		}
		dao.insert(vendor);
	}
	@Ok("tiles:admin.specialVendor.edit")
	public SpecialVendor edit(@Param("id")int id){
		return dao.fetch(SpecialVendor.class,id);
	}
	@Ok("redirect:/admin/specialVendor/list.do")
	@AdaptBy(type=UploadAdaptor.class,args={"E:/TomcatShare/temp"})
	public void update(@Param("::o.")SpecialVendor v,@Param("file")File image) throws IOException{
		if(image!=null && !Strings.isEmpty(Files.getSuffixName(image))){
			if(!Strings.isEmpty(v.getLogo())){
				Files.deleteFile(new File(ApplicationUtil.getWebHomePath()+v.getLogo()));
			}
			String imagepath = "/images/vendorlogo/"+new StringGenerator(32, 32).next()+"."+Files.getSuffixName(image);
			Files.copyFile(image, new File(ApplicationUtil.getWebHomePath()+imagepath));
			v.setLogo(imagepath);
		}
		dao.update(v);
	}
	@Ok("redirect:/admin/specialVendor/list.do")
	public void delete(@Param("id")int id){
		dao.delete(SpecialVendor.class, id);
	}
	public void deleteAll(){
		
	}
	
	@Inject
	private Dao dao ;
	public void setDao(Dao dao){
		this.dao = dao;
	}
}
